﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LootPoints.Security;
using LootPoints.Warcraft;

namespace LootPoints.Pages
{
    public partial class Characterpage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try { if (!(bool)Session["isadmin"]) { lbtnAdmin.Visible = false; } }
            catch { lbtnAdmin.Visible = false; }

            try
            {
                if (!(bool)Session["logged"]) { lbtnLogoff.Visible = false; lbtnLogin.Visible = true; }
                else { lbtnLogoff.Visible = true; lbtnLogin.Visible = false; }
            }
            catch { lbtnLogoff.Visible = false; lbtnLogin.Visible = true; }

            BattleNet armory = new BattleNet();
            
            try { LoadCharacter(armory.GetCharacter(Request.QueryString[0])); }
            catch { Response.Redirect("~/Pages/Overview.aspx"); }

            string[] character = armory.GetCharacter(Request.QueryString[0]);

            LoadRaidscore(character);
        }

        protected void lbtnLogoff_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("~/Default.aspx");
        }
        protected void lbtnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Login.aspx");
        }
        protected void lbtnAdmin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Adminpage.aspx");
        }
        protected void lbtnOverview_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Overview.aspx");
        }

        private void LoadCharacter(string[] character)
        {
            tableLeft.Rows[3].Cells[1].Text = character[0];
            tableLeft.Rows[4].Cells[1].Text = character[1];
            tableLeft.Rows[5].Cells[1].Text = character[2];
            tableLeft.Rows[6].Cells[1].Text = character[3];
            tableLeft.Rows[7].Cells[1].Text = character[6];
            tableLeft.Rows[8].Cells[1].Text = character[5];
            tableLeft.Rows[9].Cells[1].Text = character[4];

            ModelContainer context = new ModelContainer();
            string charactername = character[0];
            List<LootLog> logs = (from c in context.LootLog where c.Character.Name == charactername orderby c.Datetime descending select c).Take(10).ToList();

            foreach (LootLog log in logs)
            {
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell() { Text = "<a style='color: #4d38f8;' href='http://db.mmo-champion.com/i/" + log.ArmoryId.ToString() + "'>" + log.Item + "</a>", HorizontalAlign = HorizontalAlign.Center };
                TableCell cell2 = new TableCell() { Text = log.Datetime.ToString(), CssClass = "Cell", HorizontalAlign = HorizontalAlign.Center };
                TableCell cell3 = new TableCell() { Text = log.GP.ToString(), CssClass = "CellSmall", HorizontalAlign = HorizontalAlign.Center };

                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                row.Cells.Add(cell3);
                tableRight.Rows.Add(row);
            }
        }
        private void LoadRaidscore(string[] character)
        {
            ModelContainer context = new ModelContainer();
            string charname = character[0];
            RaidScore raidscore = (from c in context.RaidScore where c.Character.Name == charname select c).FirstOrDefault();
            RSReference reference = (from c in context.RSReference select c).FirstOrDefault();
            if (raidscore != null && reference != null)
            {
                for (int i = 0; i < reference.List.Count; i++)
                {
                    TableRow row = new TableRow();
                    row.Cells.Add(new TableCell() { Text = reference.List[i][0], HorizontalAlign = HorizontalAlign.Left });
                    row.Cells.Add(new TableCell() { Text = reference.List[i][1], HorizontalAlign = HorizontalAlign.Center });
                    row.Cells.Add(new TableCell() { Text = raidscore.List[i].ToString(), HorizontalAlign = HorizontalAlign.Center });
                    row.Cells.Add(new TableCell() { Text = (raidscore.List[i] * Convert.ToInt32(reference.List[i][1])).ToString(), HorizontalAlign = HorizontalAlign.Center });
                    tableRS.Rows.Add(row);

                }
            }
            else
            {
                tableRS.Rows.Clear();
                TableRow row = new TableRow();
                row.Cells.Add(new TableCell() { Text = "No score known, yet...", HorizontalAlign = HorizontalAlign.Center });
                tableRS.Rows.Add(row);
            }
            lblTotal.Text = raidscore.Total.ToString();
            lblAverage.Text = raidscore.Average.ToString();
            lblRemark.Text = raidscore.Remark;
        }
    }
}